package interview.huawei.tjt.training.huaweijishitiku;

import java.util.*;

// 火车进站
public class HJ77 {

    private static Stack<String> stack1 = new Stack<String>();
    private static Stack<String> stack2 = new Stack<String>();
    private static List<String> list = new ArrayList<String>();

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while ( sc.hasNext() ) {
            String n = sc.nextLine();
            String str = sc.nextLine();
            String[] train = str.split(" ");
            for ( int i = train.length - 1; i >= 0; i -- ) {
                stack1.push(train[i]);
            }
            ff("");
            Collections.sort(list);
            for(String s:list) {
                System.out.println(s);
            }
        }
    }

    public static void ff ( String str ) {

        if ( stack1.isEmpty() && stack2.isEmpty() ) {
            list.add(str.trim());
            return;
        }

        if ( !stack2.isEmpty() ) {
            String str1 = stack2.pop();
            ff(str + " " + str1);
            stack2.push(str1);
        }

        if ( !stack1.isEmpty() ) {
            String str2 = stack1.pop();
            stack2.push(str2);
            ff(str);
            stack2.pop();
            stack1.push(str2);
        }
    }

}
